package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.domain.entity.Profit;
import com.spa.infrastructure.mapper.ProfitMapperStruct;
import com.spa.infrastructure.repository.condition.ProfitCondition;
import com.spa.infrastructure.repository.mapper.ProfitMapper;
import com.spa.infrastructure.repository.po.ProfitPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ProfitRepositoryService extends ServiceImpl<ProfitMapper, ProfitPO> {

    @Autowired
    private ProfitMapper profitMapper;

    public Long add(ProfitPO po) {
        if (po.getCreateTime()== null){
            po.setCreateTime(new Date());
        }
        profitMapper.insert(po);
        return po.getId();
    }

    public List<Profit> list(ProfitCondition condition) {
        LambdaQueryWrapper<ProfitPO> wrapper = wrapper(condition);
        List<ProfitPO> profitPOS = profitMapper.selectList(wrapper);
        return ProfitMapperStruct.INSTANCE.po2EntityList(profitPOS);
    }

    public Profit getProfit(ProfitCondition condition) {
        LambdaQueryWrapper<ProfitPO> wrapper = wrapper(condition);
        ProfitPO po = profitMapper.selectOne(wrapper);
        return ProfitMapperStruct.INSTANCE.po2Entity(po);
    }


    private LambdaQueryWrapper<ProfitPO> wrapper(ProfitCondition condition) {
        LambdaQueryWrapper<ProfitPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), ProfitPO::getMassagistId, condition.getMassagistId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getOrderType()), ProfitPO::getOrderType, condition.getOrderType());
        wrapper.eq(ObjectUtil.isNotNull(condition.getOrderId()), ProfitPO::getOrderId, condition.getOrderId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), ProfitPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getMonthEndDate()), ProfitPO::getCreateTime, condition.getMonthEndDate());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getOrderIds()), ProfitPO::getOrderId, condition.getOrderIds());
        wrapper.in(ObjectUtil.isNotNull(condition.getMassagistIds()), ProfitPO::getMassagistId, condition.getMassagistIds());
        wrapper.ge(StringUtils.isNotEmpty(condition.getBeginDate()), ProfitPO::getCreateTime, condition.getBeginDate());
        wrapper.lt(StringUtils.isNotEmpty(condition.getEndDate()),  ProfitPO::getCreateTime, condition.getEndDate());
        return wrapper;
    }

    public Boolean exists(ProfitCondition profitCondition) {
        return profitMapper.exists(wrapper(profitCondition));
    }
}

